/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package IsdComponent;

import java.awt.Container;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.tree.DefaultMutableTreeNode;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author Shameera
 */
public class MakeJTree extends JFrame {

    private final ReadIsdXml readXml;
    private final Directory rootDir;
    private Directory childDir;
    private String indent = "";
    private final DefaultMutableTreeNode rootJTree;
    private DefaultMutableTreeNode childJTree;

    public MakeJTree() {
        super("JTree Selections");
        Container content = getContentPane();
        content.setLayout(new FlowLayout());
        readXml = new ReadIsdXml("E:\\ISD_edited.xml");
        Node rootNode = readXml.getRootNode();
        rootDir = new Directory(rootNode.getNodeName());  // this should have at least one sub folder
        rootJTree = new DefaultMutableTreeNode("/");
        childDir = rootDir;
        childJTree = rootJTree;
        childJTree.add(new DefaultMutableTreeNode(rootNode.getNodeName()));
        readXmlTag(rootNode);
        printDirStruc(rootDir);
        createjTree(rootDir , 0);
        this.setSize(500, 350);
        JTree tree1 = new JTree(rootJTree); // TODO add treenode
        tree1.expandRow(1); // Expand children to illustrate leaf icons
        JScrollPane pane1 = new JScrollPane(tree1);
        pane1.setSize(500, 350);
        pane1.setBorder(BorderFactory.createTitledBorder("Standard Icons"));
        content.add(pane1);
        pack();
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        setVisible(true);
    }

    private void readXmlTag(Node nodeIn) {
        NodeList nodeList = nodeIn.getChildNodes();
        int nodeLength = nodeList.getLength();
            for (int i = 1; i < nodeLength; i += 2) {
                Node node = nodeList.item(i);
                if (isDirectory(node)) {
                    Directory grandDir = new Directory(node.getNodeName());
                    childDir.addChildDir(grandDir);
                    Directory parentDir2 = childDir;
                    childDir = childDir.getLastSubDir();
                    readXmlTag(node);
                    childDir = parentDir2;
                } else {
                    System.out.println(node.getTextContent());
                    String subFile = node.getTextContent();
                    childDir.addChildFile(subFile);
                }
            }

    }

    private boolean isDirectory(Node checkNode) {
        NodeList nodeList = checkNode.getChildNodes();
        if (nodeList.getLength() == 1) {
            return false;
        } else {
            return true;
        }
    }

//    private void addfile(Node node){
//        String value = node.getNodeValue();
//        if(value!=null){
//            DefaultMutableTreeNode file = new DefaultMutableTreeNode(value);
//            childDir.add(file);
//        }
//    }
    public static void main(String[] args) {
        try {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(CustomIcons.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                Logger.getLogger(CustomIcons.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(CustomIcons.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(CustomIcons.class.getName()).log(Level.SEVERE, null, ex);
        }
        new MakeJTree();
    }

    private void printDirStruc(Directory parent) {
        indent += "-";
        for (Directory subdir : parent.getChildDirectories()) {
            System.out.println(indent + subdir.getName());
            for (String subfile : subdir.getChildFiles()) {
                System.out.println(indent + "***" + subfile);
            }
            printDirStruc(subdir);
        }
        indent = indent.substring(0, indent.length() - 1);
    }

    private void createjTree(Directory parent , int fileCount){
//                indent += "-";
                
              int noOfChild = childJTree.getChildCount();
              System.out.println(childJTree.toString());
              childJTree = (DefaultMutableTreeNode) childJTree.getChildAt(noOfChild-fileCount-1);
        for (Directory subdir : parent.getChildDirectories()) {
//            System.out.println(indent + subdir.getName());
            DefaultMutableTreeNode subjNode = new DefaultMutableTreeNode(subdir.getName());
            System.out.println("subdir is " + subdir.getName() );
            childJTree.add(subjNode);
             for (String subfile : subdir.getChildFiles()) {
//                System.out.println(indent + "***" + subfile);
                fileCount ++;
                 System.out.println("subfile is " + subfile);
                childJTree.add(new DefaultMutableTreeNode(subfile));
            }
            createjTree(subdir , fileCount);
        }
//        indent = indent.substring(0, indent.length() - 1);
        childJTree = (DefaultMutableTreeNode) childJTree.getParent();
    }
}
